using System;
using System.Drawing;
using System.Windows.Forms;

namespace F1RacingGame
{
    public class LeaderboardScreen : IGameScreen
    {
        private Font titleFont;
        private Font headerFont;
        private Font entryFont;
        private SolidBrush titleBrush;
        private SolidBrush headerBrush;
        private SolidBrush entryBrush;
        private SolidBrush backgroundBrush;
        private SolidBrush goldBrush;
        private SolidBrush silverBrush;
        private SolidBrush bronzeBrush;
        
        public void Initialize()
        {
            titleFont = new Font("Arial", 28, FontStyle.Bold);
            headerFont = new Font("Arial", 16, FontStyle.Bold);
            entryFont = new Font("Arial", 12, FontStyle.Regular);
            
            titleBrush = new SolidBrush(Color.Yellow);
            headerBrush = new SolidBrush(Color.White);
            entryBrush = new SolidBrush(Color.LightGray);
            backgroundBrush = new SolidBrush(Color.Black);
            goldBrush = new SolidBrush(Color.Gold);
            silverBrush = new SolidBrush(Color.Silver);
            bronzeBrush = new SolidBrush(Color.Orange);
        }
        
        public void Update()
        {
            // 排行榜屏幕不需要特殊更新逻辑
        }
        
        public void Render(Graphics graphics)
        {
            // 确保资源已初始化
            if (backgroundBrush == null || titleFont == null || titleBrush == null)
            {
                Initialize();
            }
            
            // 清屏
            graphics.FillRectangle(backgroundBrush, 0, 0, GameEngine.GAME_WIDTH, GameEngine.GAME_HEIGHT);
            
            // 绘制标题
            string title = "排行榜";
            SizeF titleSize = graphics.MeasureString(title, titleFont);
            float titleX = (GameEngine.GAME_WIDTH - titleSize.Width) / 2;
            graphics.DrawString(title, titleFont, titleBrush, titleX, 30);
            
            // 绘制表头
            string header = "排名  玩家名称          时间      赛车      赛道      日期";
            graphics.DrawString(header, headerFont, headerBrush, 50, 100);
            
            // 绘制分隔线
            Pen linePen = new Pen(Color.White, 2);
            graphics.DrawLine(linePen, 50, 130, GameEngine.GAME_WIDTH - 50, 130);
            linePen.Dispose();
            
            // 获取排行榜数据
            var entries = Leaderboard.Instance.GetTopEntries();
            
            // 绘制排行榜条目
            float entryY = 150;
            for (int i = 0; i < entries.Count; i++)
            {
                var entry = entries[i];
                SolidBrush rankBrush = GetRankBrush(i);
                
                string rankText = $"#{i + 1:D2}";
                string entryText = $"{rankText}  {entry.PlayerName,-12}  {entry.Time,6:F2}s  {entry.Car,-8}  {entry.Track,-8}  {entry.Date:MM-dd}";
                
                graphics.DrawString(entryText, entryFont, rankBrush, 50, entryY);
                entryY += 25;
            }
            
            // 如果没有记录
            if (entries.Count == 0)
            {
                string noRecords = "暂无记录";
                SizeF noRecordsSize = graphics.MeasureString(noRecords, headerFont);
                float noRecordsX = (GameEngine.GAME_WIDTH - noRecordsSize.Width) / 2;
                graphics.DrawString(noRecords, headerFont, entryBrush, noRecordsX, 200);
            }
            
            // 绘制提示信息
            string hint = "按ESC键返回主菜单";
            Font hintFont = new Font("Arial", 12, FontStyle.Regular);
            SolidBrush hintBrush = new SolidBrush(Color.Gray);
            SizeF hintSize = graphics.MeasureString(hint, hintFont);
            float hintX = (GameEngine.GAME_WIDTH - hintSize.Width) / 2;
            graphics.DrawString(hint, hintFont, hintBrush, hintX, GameEngine.GAME_HEIGHT - 50);
            
            hintFont.Dispose();
            hintBrush.Dispose();
        }
        
        private SolidBrush GetRankBrush(int rank)
        {
            switch (rank)
            {
                case 0: return goldBrush;      // 金牌
                case 1: return silverBrush;    // 银牌
                case 2: return bronzeBrush;    // 铜牌
                default: return entryBrush;    // 其他
            }
        }
        
        public void OnKeyDown(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    GameManager.Instance.ChangeState(GameState.MainMenu);
                    break;
            }
        }
        
        public void OnKeyUp(KeyEventArgs e)
        {
            // 排行榜屏幕不需要处理按键释放
        }
        
        public void Dispose()
        {
            titleFont?.Dispose();
            headerFont?.Dispose();
            entryFont?.Dispose();
            titleBrush?.Dispose();
            headerBrush?.Dispose();
            entryBrush?.Dispose();
            backgroundBrush?.Dispose();
            goldBrush?.Dispose();
            silverBrush?.Dispose();
            bronzeBrush?.Dispose();
        }
    }
} 